package com.waimai.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.waimai.constant.DaoConstant;
import com.waimai.log.Log;

public class DBManager {
	// 别静态，会出错
	private Connection conn = null;
	static {
		try {
			Class.forName(DaoConstant.DB_DRIVER);
		} catch (ClassNotFoundException e) {
			Log.e(e, "dbDriver get error");
			e.printStackTrace();
		}
	}

	public Connection getConnection() {
		try {
			if (conn == null) {
				conn = DriverManager.getConnection(DaoConstant.DB_URL,
						DaoConstant.DB_USER, DaoConstant.DB_PWD);
			}
		} catch (SQLException e) {
			Log.e(e, "connettion get error");
			e.printStackTrace();
		}
		return conn;
	}

	public void releaseConnection() {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				Log.e(e, "sqlConnection close error");
				e.printStackTrace();
			} finally {
				conn = null;
			}
		}
	}
}
